 //
//  DDCityChooseView.m
//  DingDongZP
//
//  Created by Jyh on 16/5/8.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDCommonDataManager.h"
#import "DDCommonDataModel.h"
#import "DDCityChooseView.h"

static CGFloat kPickerViewHeight = 161.f;

@interface DDCityChooseView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *pickerView;

@property (strong, nonatomic) UIView *toolsView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *cancelButton;
@property (strong, nonatomic) UIButton *saveButton;

@property (nonatomic, strong) NSMutableArray *provinces;

@property (nonatomic, assign) NSInteger proIndex;

@property (copy, nonatomic) NSString *latestCityName;
@property (copy, nonatomic) NSString *latestCityId;

@end

@implementation DDCityChooseView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
        self.frame = frame;
        self.pickerView.frame = CGRectMake(0 ,kScreenHeight - 64 - kPickerViewHeight , kScreenWidth, kPickerViewHeight);
        self.toolsView.frame = CGRectMake(0, CGRectGetMidY(self.pickerView.frame) - 44 - 64, kScreenWidth, 44);
        
        [self.toolsView addSubview:self.cancelButton];
        [self.toolsView addSubview:self.titleLabel];
        [self.toolsView addSubview:self.saveButton];
        
        [self addSubview:self.pickerView];
        [self addSubview:self.toolsView];
        
        [self bk_whenTapped:^{
            if (!self.hidden) {
                [self hideChooseView];
            }
        }];
        
        [self loadData];
    }
    return self;
}

- (void)loadData
{
    WEAKSELF
     [[DDCommonDataManager shareManager] getProvincesArrayWithBlock:^(NSArray *provinceArray) {
         weakSelf.provinces = [NSMutableArray arrayWithArray:provinceArray];
         [weakSelf.pickerView reloadAllComponents];
     }];
}

#pragma mark
#pragma mark - Public Methods

- (void)hideChooseView
{
    self.hidden = YES;
}

- (void)showChooseView
{
    self.hidden = NO;
}


#pragma mark
#pragma mark - Event Response

- (void)onCancelClick:(UIButton *)button
{
    [self hideChooseView];
}

- (void)onChooseClick:(UIButton *)button
{
    if (self.selectedBlock) {
        self.selectedBlock(self.latestCityName, self.latestCityId);
        [self hideChooseView];
    } else {
        [self hideChooseView];
    }
}

#pragma mark
#pragma mark - CustomDelegate

#pragma mark UIPickerView 数据源
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
// 某一列行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)  // 省会
    {
        return [self.provinces count];
    }
    else  // 其他城市
    {
        DDCommonDataModel *model = self.provinces[_proIndex];

        return model.cities.count;
    }
}

// 每行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        // 获取省会
        DDCommonDataModel *model = self.provinces[row];
        return model.province_name;
    }
    else  // 其他城市
    {
        DDCommonDataModel *model = self.provinces[_proIndex];
        DDCityModel *cityModel = model.cities[row];
        return cityModel.city_name;
    }
}

// 是否选中某行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // 滚动省会刷新城市
    if(component == 0)
    {
        // 记录当前省会
        _proIndex = [pickerView selectedRowInComponent:0];
        [pickerView reloadComponent:1];
    }
    
    // 获取选中省会
    DDCommonDataModel *model = self.provinces[_proIndex];
    NSInteger cityIndex = [pickerView selectedRowInComponent:1];
    DDCityModel *cityModel = model.cities[cityIndex];
    NSLog(@"选中的城市id  %@ 城市名称 %@",cityModel.city_id,cityModel.city_name);
    self.latestCityId = cityModel.city_id;
    self.latestCityName = cityModel.city_name;
}


#pragma mark
#pragma mark - Getters and Setters

- (UIPickerView *)pickerView
{
    if (_pickerView == nil) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
    }
    
    return _pickerView;
}

- (UIView *)toolsView
{
    if (_toolsView == nil) {
        _toolsView = [[UIView alloc] init];
        _toolsView.backgroundColor = COLOR_BACKGROUND;
    }
    
    return _toolsView;
}

- (UIButton *)cancelButton
{
    if (_cancelButton == nil) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setImage:[UIImage imageNamed:@"common_back"] forState:UIControlStateNormal];
        _cancelButton.frame = CGRectMake(10, 12, 20, 20);
        [_cancelButton addTarget:self action:@selector(onCancelClick:) forControlEvents:UIControlEventTouchDown];
    }
    
    return _cancelButton;
}

- (UIButton *)saveButton
{
    if (_saveButton == nil) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setImage:[UIImage imageNamed:@"common_check"] forState:UIControlStateNormal];
        _saveButton.frame = CGRectMake(kScreenWidth - 47, 9, 37, 25);
        [_saveButton addTarget:self action:@selector(onChooseClick:) forControlEvents:UIControlEventTouchDown];
    }
    
    return _saveButton;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = COLOR_FONT_DARK_GRAY;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.frame = CGRectMake(70, 10, kScreenWidth - 2*70, 24);
        _titleLabel.text = @"城市选择";
    }
    
    return _titleLabel;
}

- (NSMutableArray *)provinces
{
    if (_provinces == nil) {
        _provinces = [[NSMutableArray alloc] initWithCapacity:0];
    }
    
    return _provinces;
}

@end
